package com.sjtu.vfact.textual.algorithm;

import java.util.List;

import com.sjtu.vfact.textual.model.Document;
import com.sjtu.vfact.textual.model.FACTAModel;
import com.sjtu.vfact.textual.model.MasterCollection;
import com.sjtu.vfact.textual.model.Pair;
import com.sjtu.vfact.textual.model.TermSet;
import com.sjtu.vfact.textual.util.TextUtil;

public class Facta_D extends Facta{
	
	private FACTAModel model;
	
	public Facta_D(FACTAModel model){
		super();
		this.model = model;
	}
	
	
	public MasterCollection run(List<Document> docs, int L_min, int L_max, int N, int p) {
		MasterCollection collection = new MasterCollection();
		
		if(!(docs.isEmpty())){
			int totalDocs = docs.size();
			for(int i = 0; i < totalDocs; i++){
				Document doc_i = docs.get(i);
				List<String> terms_i = doc_i.getTerms();
				int j;
				for(int k = i + 1; k <= i + p; k++){
					
					if(k >= totalDocs){
						j = k % totalDocs;
					}else{
						j = k;
					}
					
					Document doc_j = docs.get(j);
					List<String> terms_j = doc_j.getTerms();
					List<String> termsIntersect = TextUtil.<String>intersect(terms_i, terms_j);
					if(termsIntersect.size() >= L_min && termsIntersect.size() <= L_max){

						TermSet termset = new TermSet(termsIntersect);
						int support = model.calculateTermsetSupport(termset);
						Pair<Integer, Integer> docPair = new Pair<Integer, Integer>(doc_i.getId(), doc_j.getId());
						collection.add(termset, support, docPair);
					}
					
					if(collection.getSize() > N){
						return collection;
					}	
					
				}
				
				setProgress((100 * (i+1)) / totalDocs);
				setActualDocument(i + 1);
			}
		}
		
		return collection;
	}

}
